package cn.lg.soar.system.account.controller;

import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.common.util.current.UserContext;
import cn.lg.soar.core.annotations.ResourceScanIgnore;
import cn.lg.soar.core.annotations.constraints.Id;
import cn.lg.soar.database.query.ConditionEnum;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.database.query.SqueryWapper;
import cn.lg.soar.mvc.annotations.Log;
import cn.lg.soar.system.account.model.*;
import cn.lg.soar.system.account.service.IAccountService;
import cn.lg.soar.system.account.service.ILoginService;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.api.model.*;
import cn.lg.soar.system.auth.model.MenuTree;
import cn.lg.soar.system.monitor.entity.OperationLog;
import cn.lg.soar.system.monitor.service.OperationLogService;
import cn.lg.soar.system.tenant.entity.Tenant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@ResourceScanIgnore
@Api(tags = {"账号信息"})
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/account")
public class AccountController {

    @Autowired
    private IAccountService accountService;
    @Autowired
    private ILoginService loginService;
    @Autowired
    private OperationLogService operationLogService;

    @ApiOperation("用户账号信息")
    @GetMapping(value = "info", name = "用户账号信息")
    public HttpResult<AccountInfoVo> accountInfo(
            @ApiParam("查询类型") AccountInfoQueryEnum[] queryEnums,
            @ApiParam("系统标识（用于菜单）") String[] systems
    ) {
        if (queryEnums == null) {
            queryEnums = new AccountInfoQueryEnum[]{
                    AccountInfoQueryEnum.user,
                    AccountInfoQueryEnum.role,
                    AccountInfoQueryEnum.menu
            };
        }
        return HttpResult.data(this.accountService.getAccountInfo(queryEnums, systems));
    }

    @ApiOperation("菜单")
    @GetMapping(value = "menus", name = "菜单")
    public HttpResult<List<MenuTree>> menus(String[] systems) {
        return HttpResult.data(this.accountService.menus(systems));
    }

    @ApiOperation("角色")
    @GetMapping(value = "roles", name = "角色")
    public HttpResult<List<AuthRole>> roles() {
        return HttpResult.data(this.accountService.roles());
    }

    @ApiOperation("组织机构")
    @GetMapping(value = "orgs", name = "组织机构")
    public HttpResult<List<AuthOrg>> orgs() {
        return HttpResult.data(this.accountService.orgs());
    }

    @ApiOperation("部门列表")
    @GetMapping(value = "depts", name = "部门列表")
    public HttpResult<List<AuthDept>> depts() {
        return HttpResult.data(this.accountService.depts());
    }

    @ApiOperation("岗位")
    @GetMapping(value = "posts", name = "岗位")
    public HttpResult<List<AuthPost>> posts() {
        return HttpResult.data(this.accountService.posts());
    }

    @ApiOperation("绑定的账号")
    @GetMapping(value = "accounts", name = "绑定的账号")
    public HttpResult<List<AuthAccountBind>> accounts() {
        return HttpResult.data(this.accountService.accounts());
    }

    @ApiOperation("租户列表")
    @GetMapping(value = "tenants", name = "租户列表")
    public HttpResult<List<Tenant>> tenants() {
        return HttpResult.data(this.accountService.tenants());
    }

    @Log(value = "切换租户")
    @ApiOperation("切换租户")
    @PostMapping(value = "change-tenant", name = "切换租户")
    public HttpResult<String> changeTenant(@RequestBody @Validated ChangeTenantVO vo) {
        return HttpResult.data(
                this.accountService.changeTenant(vo)
        );
    }

    @Log(value = "修改密码")
    @ApiOperation("修改个人密码")
    @PostMapping(value = "update-password", name = "修改个人密码")
    public HttpResult<Void> updatePassword(@RequestBody @Validated UpdatePasswordDTO userPassword) {
        this.accountService.updatePassword(userPassword);
        return HttpResult.success();
    }

    @Log(value = "修改个人信息", data = "#p0")
    @ApiOperation("修改个人信息")
    @PostMapping(value = "update-info", name = "修改个人信息")
    public HttpResult<Void> updateInfo(@ApiParam @RequestBody UpdateUserInfoDTO userInfo) {
        accountService.updateInfo(userInfo);
        return HttpResult.success();
    }

    @Log(value = "退出登录")
    @ApiOperation("退出登录")
    @PostMapping(value = "logout", name = "退出登录")
    public HttpResult<Void> logout() {
        this.loginService.logout();
        return HttpResult.success("退出登录成功");
    }

    @ApiOperation("操作日志")
    @GetMapping(value = "log", name = "操作日志")
    public HttpResult<PageResult<OperationLog>> query(PageQuery pageQuery, QueryParams params){
        params.addFilter(OperationLog::getUserId, ConditionEnum.eq, UserContext.getUserId());
        return HttpResult.data(
                new SqueryWapper<>(OperationLog.class).query(this.operationLogService, pageQuery, params)
        );
    }

    @ApiOperation("操作日志")
    @GetMapping(value = "log/detail", name = "操作日志")
    public HttpResult<OperationLog> detail(@Id Long id) {
        return HttpResult.data(this.operationLogService.getById(id));
    }

    @ApiOperation("解绑账号")
    @PostMapping(value = "unbind-account", name = "解绑账号")
    public HttpResult<Void> unbindAccount(@RequestBody @Valid UnbindAccountVO vo) {
        accountService.unbindAccount(vo);
        return HttpResult.success();
    }

}
